﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.Data.PLinq.Helpers;
using DevExpress.UnitConversion;
using DevExpress.XtraEditors;
using Zero.POCO.Enum;
using Zero.Presenter.Donnees;

namespace DevExpress.MailClient.Win.Forms.Data
{
    public partial class frmEditLigneTarifs : DevExpress.XtraEditors.XtraForm
    {

        private bool _newLigne = true;
        private Guid Lignetarifid { get; set; }
        public LignesTarif LigneTarif { get; set; }

        public TarifView tarifview { get; set; }
        private bool _isligneTarifModified { get; set; }
        List<TarifView> _tarifViews { get; set; }
        public frmEditLigneTarifs(LignesTarif ligneTarifHist,List<TarifView> tarifViews,bool newLigneTarif,int focusedRow)
        {
            InitializeComponent();DialogResult = DialogResult.Cancel;
            _newLigne = newLigneTarif;
            _tarifViews = tarifViews;

            luTarif.Properties.DataSource = _tarifViews;

            luTarif.Properties.DisplayMember = "Nom";
            timeFin.Time=new DateTime(2014,1,1,1,0,0);
            cbJour.SelectedIndex = 0;
            if(_tarifViews.Any())
            {
                luTarif.EditValue = _tarifViews.FirstOrDefault();
            


                switch (_tarifViews[focusedRow].media)
                {
                    case TypeMedia.TV:
                        {
                            timeDebut.Enabled = true;
                            timeFin.Enabled = true;
                            var listeDonne = new List<string>();
                            listeDonne.Add("Tout format");
                            for (int i = 1; i < 20; i++)
                            {
                                listeDonne.Add(5 * i + " secondes");
                            }
                            luFormat.Properties.DataSource = listeDonne;
                            luFormat.EditValue = listeDonne.FirstOrDefault();
                            break;
                        }

                    case TypeMedia.RD:
                        {
                            timeDebut.Enabled = true;
                            timeFin.Enabled = true;
                            var listeDonne = new List<string>();
                            listeDonne.Add("Tout format");
                            for (int i = 1; i < 20; i++)
                            {
                                listeDonne.Add(5 * i + " secondes");
                            }
                            luFormat.Properties.DataSource = listeDonne;
                            luFormat.EditValue = listeDonne.FirstOrDefault();
                            break;
                        }
                    case TypeMedia.PR:
                    {
                        timeDebut.Enabled = false;
                        timeFin.Enabled = false;
                        luFormat.Properties.DataSource = Enum.GetValues(typeof(FormatPresse));
                        luFormat.EditValue = FormatPresse.ToutFormat;

                            break;
                        }
                }
            }
            if (_tarifViews.Any())
                luTarif.ItemIndex = focusedRow;
            var test = luTarif.EditValue;



            
            
            if (_newLigne)
            {
                Lignetarifid = default(Guid);
                _isligneTarifModified = true;
            }
            else
                Lignetarifid = ligneTarifHist.id;
        }

        DialogResult QueryDelete()
        {
            DialogResult result = XtraMessageBox.Show(this, Properties.Resources.DeleteQuestion, Application.ProductName, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation);
            switch (result)
            {
                case DialogResult.Cancel:
                    return DialogResult.Cancel;
                case DialogResult.No:
                    return DialogResult.No;
                default:
                    DeleteRecord();
                    return DialogResult.Yes;
            }
        }

        private void DeleteRecord()
        {
            throw new NotImplementedException();
        }

        DialogResult QueryClose()
        {
            if (!_isligneTarifModified)
                return DialogResult.Yes;

            DialogResult result = XtraMessageBox.Show(this, Properties.Resources.SaveQuestion, Application.ProductName, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation);
            switch (result)
            {
                case DialogResult.Cancel:
                    return DialogResult.Cancel;
                case DialogResult.No:
                    return DialogResult.No;
                default:
                    ApplyChanges();
                    return DialogResult.Yes;
            }
        }

        private void ApplyChanges()
        {


            
            
            var debutper = TimeSpan.FromTicks(timeDebut.Time.Ticks);
            var debtime = new TimeSpan(0,debutper.Hours,debutper.Minutes,debutper.Seconds);


            var finper = TimeSpan.FromTicks(timeFin.Time.Ticks);
            var fintime = new TimeSpan(0,finper.Hours,debutper.Minutes,debutper.Seconds);


           
            LigneTarif = new LignesTarif()
            {
                id = Lignetarifid,
          
                JourSemaine = cbJour.SelectedIndex,
                Prix = double.Parse(tePrix.Text),
               
            };


            LigneTarif.Debut = debtime;

            LigneTarif.Fin = fintime;
            LigneTarif.Format = luFormat.ItemIndex;
            tarifview = (TarifView)luTarif.EditValue;

        }

        private void simpleButton1_Click(object sender, EventArgs es)
        {

            var debutper = TimeSpan.FromTicks(timeDebut.Time.Ticks);
            var debtime = new TimeSpan(0, debutper.Hours, debutper.Minutes, debutper.Seconds);


            var finper = TimeSpan.FromTicks(timeFin.Time.Ticks);
            var fintime = new TimeSpan(0, finper.Hours, debutper.Minutes, debutper.Seconds);

            
            if(debtime>=fintime) return;
            var tarif = _tarifViews.First(ed => ed.id == ((TarifView)luTarif.EditValue).id);
            IEnumerable<LignesTarif> lignes = new List<LignesTarif>();
            if (tarif.lignetarif!=null)
              lignes= tarif.lignetarif.Where(
                   e => e.JourSemaine == cbJour.SelectedIndex && e.Format == luFormat.ItemIndex &&
                       ((e.Debut <= debtime && e.Fin >= debtime) || (e.Debut <= fintime && e.Fin >= fintime)));

            if (lignes.Any()) return;
            _isligneTarifModified= false;
            DialogResult result = QueryClose();
            ApplyChanges();
            if (result == DialogResult.Yes) this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult result = QueryClose();
            this.Close();
        }

        private void luTarif_EditValueChanged(object sender, EventArgs e)
        {
            var test = luTarif.EditValue;
            tarifview = ((TarifView) test);var typeMedia = ((TarifView)test).media;


            

             switch (typeMedia)
            {
                case TypeMedia.TV:
                {timeDebut.Enabled = true;
                    timeFin.Enabled = true;
                    var listeDonne = new List<string>();
                    listeDonne.Add("Tout format");
                    for (int i = 1; i < 20; i++){
                        listeDonne.Add(5 * i + " secondes");
                    }
                    luFormat.Properties.DataSource = listeDonne;
                    luFormat.EditValue = listeDonne.FirstOrDefault();
                    tarifview = (TarifView)luTarif.EditValue;
                    break;
                }

                case TypeMedia.RD:
                {
                    timeDebut.Enabled = true;
                    timeFin.Enabled = true;
                    var listeDonne = new List<string>();
                    listeDonne.Add("Tout format");
                    for (int i = 1; i < 20; i++)
                    {
                        listeDonne.Add(5 * i + " secondes");
                    }
                    luFormat.Properties.DataSource = listeDonne;
                    luFormat.EditValue = listeDonne.FirstOrDefault();
                    tarifview = (TarifView)luTarif.EditValue;
                    break;
                }
                 case TypeMedia.PR:
                {
                    timeDebut.Enabled = false;
                    timeFin.Enabled = false;
                    luFormat.Properties.DataSource = Enum.GetValues(typeof(FormatPresse));
                    luFormat.EditValue = FormatPresse.ToutFormat;
                    tarifview = (TarifView)luTarif.EditValue;
                    break;
                }
            }









             _isligneTarifModified = true;
        }

        private void timeDebut_Properties_Validating(object sender, CancelEventArgs e)
        {
            if (timeDebut.Time <= timeFin.Time) e.Cancel = false;
        }

        private void timeFin_Properties_Validating(object sender, CancelEventArgs e)
        {
            if ( timeDebut.Time >= timeFin.Time) e.Cancel = false;
        }

        private void luTarif_Properties_Validating(object sender, CancelEventArgs e)
        {
            if (luTarif.EditValue == "Selectionner valeur") e.Cancel = false;
        }

        private void luFormat_Properties_Validating(object sender, CancelEventArgs e)
        {
            if (luFormat.EditValue == "Selectionner valeur") e.Cancel = false;
        }
        private void tePrix_Properties_Validating(object sender, CancelEventArgs e)
        {
            if (tePrix.Text.Trim().Length <= 0)
                e.Cancel = true;
        }

        private void timeDebut_EditValueChanged(object sender, EventArgs e)
        {
            _isligneTarifModified = true;
        }

        private void timeFin_EditValueChanged(object sender, EventArgs e)
        {
            _isligneTarifModified = true;
        }

        private void cbJour_EditValueChanged(object sender, EventArgs e)
        {
            _isligneTarifModified = true;
        }

        private void luFormat_EditValueChanged(object sender, EventArgs e)
        {
            _isligneTarifModified = true;
        }
        private void tePrix_EditValueChanged(object sender, EventArgs e)
        {
            _isligneTarifModified = true;
        }

        private void timeDebut_Validating(object sender, CancelEventArgs e)
        {
            if (timeDebut.Time <= timeFin.Time) e.Cancel = false;}

        private void luTarif_Popup(object sender, EventArgs e)
        {
            //luTarif.Properties.PopulateColumns();
            luTarif.Properties.Columns[0].Visible = false;
            luTarif.Properties.Columns[3].Visible = false;
            luTarif.Properties.Columns[4].Visible = false;
        }

        private void frmEditLigneTarifs_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult=QueryClose();
        }
    }
}